package com.yami.shop.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yami.shop.bean.model.Comment;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.param.CommentParam;
import com.yami.shop.bean.param.PostsQuery;
import com.yami.shop.bean.vo.CommentVO;
import com.yami.shop.dao.CommentMapper;
import com.yami.shop.dao.UserMapper;
import com.yami.shop.service.CommentService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;


/**
 * <p>
 * 用于存储用户对动态的评论 服务实现类
 * </p>
 *
 * @author EXiaoKe
 * @since 2025-04-14
 */
@Service
public class TzCommentsServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Resource
    private UserMapper userMapper;

    @Resource
    private CommentMapper commentMapper;


    @Override
    public IPage<CommentVO> getPostPage(Page<Comment> page, PostsQuery query) {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<Comment>()
                .eq(query.getStatus() != null, Comment::getContent, query.getStatus())
                .like(StrUtil.isNotBlank(query.getContent()), Comment::getContent, query.getContent());

        // 1. 执行原始分页查询（Post表）
        Page<Comment> postPage = baseMapper.selectPage(page, wrapper);

        // 2. 提取所有userId准备批量查询
        List<String> userIds = postPage.getRecords().stream()
                .map(Comment::getUserId)
                .distinct()
                .collect(Collectors.toList());

        final Map<String, String> userNickNameMap = userIds.isEmpty()
                ? Collections.emptyMap()  // 直接返回空 Map，无需初始化后修改
                : userMapper.selectBatchIds(userIds).stream()
                .collect(Collectors.toMap(User::getUserId, User::getNickName));

        return postPage.convert(post -> {
            CommentVO vo = new CommentVO();
            BeanUtils.copyProperties(post, vo);
            vo.setNickName(userNickNameMap.getOrDefault(post.getUserId(), "未知用户")); // ✅ 变量隐式 final


            return vo;

        });

    }

    /**
     * 获取评论列表
     *
     * @param postId
     * @return
     */
    @Override
    public Stream<Comment> getCommentsByPost(Long postId) {
        //根据postId查询评论列表
        List<Comment> comments = commentMapper.selectList(new LambdaQueryWrapper<Comment>().eq(Comment::getPostId, postId));
        if (comments.isEmpty()) {
            return null;
        }
        return comments.stream();
    }

    /**
     * 添加评论
     *
     * @param param
     * @return
     */
    @Override
    public Long addComment(CommentParam param) {
        Comment comment = new Comment();
        comment.setContent(param.getContent());
        comment.setPostId(param.getPostId());
        comment.setUserId(param.getUserId());
        if (commentMapper.insert(comment) > 0) {
            return comment.getCommentId();
        }

        return null;

    }

}
